package com.adjy.pathbuilders.screens;

import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

import com.adjy.pathbuilders.objects.GameEntityType;
import com.adjy.pathbuilders.rendering.AnimationPack;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.loading.AnimationFactory;
import com.adjy.pathbuilders.world.GameWorld;

public class ScoreScreen implements GameScreen {
	
	private Paint defaultPaint;
	private AnimationFactory animationFactory;
	private GameWorld parent;
	private AnimationPack coinPack;
	private AnimationPack lifePack;
	private AnimationPack timePack;
	static final int POS_X_COIN = 5;
	static final int POS_Y_COIN = 60;
		
	public ScoreScreen(AnimationFactory anFactory, GameWorld parent) {
		this.parent = parent;
		this.animationFactory = anFactory;
	}
	
	/**
	 * Creates the default paint, which can write simple messages in the screen.
	 */
	private void createDefaultPaint() {
		this.defaultPaint = new Paint();
		this.defaultPaint.setColor(Color.RED);
		this.defaultPaint.setTextSize(20);
		//FIXME Italic
		this.defaultPaint.setAntiAlias(true);
	}
	
	private void createDisplay() {
		plotTime();
		plotLives();
		plotScore();
	}

	private void plotScore() {
		this.coinPack = this.animationFactory.getAnimationsFor(GameEntityType.COIN); // 50, 600
	}

	private void plotLives() {
		this.lifePack = this.animationFactory.getAnimationsFor(GameEntityType.LIFE); // 150, 600
	}

	private void plotTime() {
		this.timePack = this.animationFactory.getAnimationsFor(GameEntityType.TIMECLOCK); // 250, 600
	}

	private int getCurrentTime() {
		return (int) this.parent.getCurrentTime();
	}

	@Override
	public void render(GameCanvas canvas, float timeElapsed) {
		canvas.drawBitmapFromPhysics(this.timePack.getAnimation(
				GameEntityType.getDefaultAnimationName(GameEntityType.TIMECLOCK)).getCurrentFrame(timeElapsed),
				25, 60);
		plotTimeCounter(canvas);
		canvas.drawBitmapFromPhysics(this.coinPack.getAnimation(
				GameEntityType.getDefaultAnimationName(GameEntityType.COIN)).getCurrentFrame(timeElapsed),
				POS_X_COIN, POS_Y_COIN);
		plotScoreCounter(canvas);
		canvas.drawBitmapFromPhysics(this.lifePack.getAnimation(
				GameEntityType.getDefaultAnimationName(GameEntityType.LIFE)).getCurrentFrame(timeElapsed),
				15, 60);
		plotLivesCounter(canvas);
	}

	private void plotLivesCounter(GameCanvas canvas) {
		canvas.drawTextWithConversion(""+this.parent.getMainChar().getLives(), 150, 40, this.defaultPaint);
	}

	private void plotScoreCounter(GameCanvas canvas) {
		if (this.parent.getMainChar() == null) {
			System.out.println("MainChar is NULL");
		}
		canvas.drawTextWithConversion(""+this.parent.getMainChar().getPoints(), 70, 40, this.defaultPaint);
	}

	private void plotTimeCounter(GameCanvas canvas) {
		canvas.drawTextWithConversion(String.valueOf(getCurrentTime()), 250, 40, this.defaultPaint);
	}

	@Override
	public void load() {
		createDefaultPaint();
		createDisplay();
	}

	@Override
	public void release() {
		// GameWorld should take care of unloading Animations
	}

	@Override
	public void input(MotionEvent event) {
	}

	@Override
	public void step(float timePassed) {
	}
	
}
